import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

part 'bt_api_result.g.dart';

@JsonSerializable(genericArgumentFactories: true)
class BtApiResult<T> {
  const BtApiResult({
    required this.code,
    required this.key,
    required this.data,
    required this.msg,
  });

  factory BtApiResult.fromJson(Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
      _$BtApiResultFromJson(json, fromJsonT);

  static const success = 0;
  static const unknown = -1;
  final int code;
  final String key;
  final String msg;
  final T? data;

  bool get isSuccess => code == success;

  Map<String, dynamic> toJson(Object? Function(T) toJsonT) => _$BtApiResultToJson(this, toJsonT);

  @override
  String toString() => 'BtApiResult{code: $code, data: ${json.encode(data)}, msg: $msg}';
}
